<template>
  <div class="detail">
    <div class="breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>
          <a href="/">{{details.majorCate}}</a>
        </el-breadcrumb-item>
        <el-breadcrumb-item>{{details.title}}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="detail-body">
      <img :src="imgUrl+details.cover" alt class="detail-body-img1" />
      <div class="detail-body-right">
        <div class="detail-body-right-top">
          <span>{{details.title}}</span>
          <span class="p2">文/{{details.author}}</span>
          <p class="mobile" @click="kehuduan">
            <i class="el-icon-mobile"></i> 手机阅读
          </p>
        </div>
        <ul class="ul">
          <li class="li" v-for="(item,index) in details.tags" :key="index">{{item}}</li>
        </ul>
        <p class="p3">
          <span>类别:{{details.cat}}</span>
          <span class="span1"></span>
          <span>字数:{{wordCount}}万字</span>
          <el-rate v-model="value" show-text class="value"></el-rate>
        </p>
        <div class="detail-body-bottom">
          <div class="detail-body-bottom-left">
            <p
              class="updata"
              @click="readbook(chapters[chapters.length-1].link)"
            >最近更新:{{details.lastChapter}}</p>
            <p class="p4">最近更新时间:{{updated}}</p>
            <div class="btn">
              <el-button type="danger" @click="readbook(chapters[0].link)">
                <span class="startread">开始阅读</span>
              </el-button>
              <el-button :plain="true" @click="open2">加入书架</el-button>
            </div>
          </div>
          <div class="detail-body-bottom-right">
            <p>
              追书人数:
              <span>{{details.latelyFollower}}</span>
            </p>
            <p>
              读者存留率:
              <span>{{details.retentionRatio}}</span>
            </p>
            <p>
              日更新字数:
              <span>{{details.serializeWordCount}}</span>
            </p>
          </div>
        </div>
      </div>
    </div>
    <div class="detail-center">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="作品简介" name="first">
          <div class="first">
            <div class="descripton">
              <h3>《{{details.title}}》简介：</h3>
              <p class="content" v-html="longIntro"></p>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="作品最新章节" name="second">
          <ul class="ul1">
            <li
              v-for="(item,index) in lastchapters"
              :key="index"
              class="li1"
              @click="readbook(item.link)"
            >{{item.title}}</li>
          </ul>
        </el-tab-pane>
        <el-tab-pane label="作品目录" name="third">
          <ul class="ul1">
            <li
              v-for="(item,index) in chapters"
              :key="index"
              class="li1"
              @click="readbook(item.link)"
            >{{item.title}}</li>
          </ul>
        </el-tab-pane>
      </el-tabs>
    </div>
    <div class="detail-bottom">
      <img src="../../assets/lh/imgs/xianli.jpg" alt />
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getDetails, getId, getChapters } from "../../api/lh";
export default {
  data() {
    return {
      _id: "",
      details: {},
      cover: "",
      imgUrl: "http://statics.zhuishushenqi.com",
      updated: "",
      wordCount: "",
      value: null,
      activeName: "first",
      longIntro: "",
      chapters: [],
      lastchapters: []
    };
  },
  computed: {
    ...mapState(["messAge"])
  },
  created() {
    this._id = localStorage.getItem("_id");
    getDetails(this._id)
      .then(res => {
        console.log(res);
        this.details = res.data;
        this.updated = this.details.updated.slice(0, 10);
        this.wordCount = this.details.wordCount.toString().slice(0, 3);
        this.longIntro = this.details.longIntro.replace(
          /\n|\r\n/g,
          "<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
        );
      })
      .catch(err => {
        alert("网络请求错误");
      });
    getId(this._id)
      .then(res => {
        console.log(res);

        console.log(res.data[0]._id);
        getChapters(res.data[0]._id).then(res => {
          console.log(res);
          this.chapters = res.data.chapters;
          console.log(this.chapters);
          this.lastchapters = this.chapters.slice(-9).reverse();
        });
      })
      .catch(err => {
        alert("网络请求错误");
      });
  },
  methods: {
    readbook(link) {
      localStorage.setItem("link", link);
      this.$router.push("/readybooks");
    },
    kehuduan() {
      this.$router.push("/kehuduan");
    },

    open2() {
      this.$message({
        message: "恭喜你，成功将书本添入书架！",
        type: "success"
      });
      this.$store.commit("getMessage", this.details);
    },
    handleClick(tab, event) {
      console.log(tab, event);
    }
  }
};
</script>

<style scoped>
.detail {
  width: 1200px;
  background-color: #fff;
  overflow: hidden;
  margin: 0 auto;
}
.breadcrumb {
  width: 1200px;
  margin: 20px 0;
}
.detail-body {
  width: 1000px;
  height: 287px;
  overflow: hidden;
}
.detail-body-img1 {
  width: 210px;
  height: 287px;
  float: left;
}
.detail-body-right {
  width: 700px;
  height: 287px;
  margin-left: 30px;
  float: left;
}
.detail-body-right-top {
  width: 700px;
  height: 71px;
  line-height: 71px;
}
.detail-body-right-top span:nth-of-type(1) {
  font-size: 30px;
}
.detail-body-right-top span:nth-of-type(2) {
  margin-left: 30px;
}
.detail-body-right-top .mobile {
  float: right;
  margin-top: 10px;
  color: lightseagreen;
  cursor: pointer;
}
.ul {
  overflow: hidden;
  height: 30px;
}
.ul .li {
  float: left;
  margin-right: 20px;
  font-size: 18px;
  line-height: 30px;
}
.detail-body-bottom {
  width: 800px;
  overflow: hidden;
}
.detail-body-bottom-left {
  float: left;
}
.detail-body-bottom-right {
  float: left;
  margin-left: 150px;
}

.detail-body-bottom-right p:nth-of-type(1) {
  margin-top: 10px;
}
.detail-body-bottom-right p:nth-of-type(2) {
  margin-top: 30px;
}
.detail-body-bottom-right p:nth-of-type(3) {
  margin-top: 30px;
}
.detail-body-bottom-right p span {
  color: hotpink;
}
.p3 {
  height: 50px;
  line-height: 50px;
}
.p3 span:nth-of-type(2) {
  border-right: 2px solid hotpink;
  margin: 0 10px;
}
.value {
  float: right;
  margin-top: 10px;
}
.updata {
  height: 40px;
  line-height: 40px;
  cursor: pointer;
}
.p4 {
  height: 50px;
  font-size: 16px;
  line-height: 50px;
}
.btn {
  margin-top: 6px;
}
.detail-center {
  margin: 40px 0 60px 0;
  width: 1200px;
}
.descripton {
  margin-left: 30px;
}
.ul1 {
  overflow: hidden;
}
.li1 {
  float: left;
  width: 400px;
  font-size: 14px;
  margin-bottom: 10px;
  cursor: pointer;
}
.startread {
  color: white;
}
</style>